package com.example.himalayaproject.find.broadcast.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.himalayaproject.R;
import com.example.himalayaproject.find.broadcast.adapter.GridItemAdapter;
import com.example.himalayaproject.find.broadcast.bean.grid.GridEntity;
import com.example.himalayaproject.find.broadcast.callback.BroadcastHttpService;
import com.example.himalayaproject.find.broadcast.callback.OnPageLoadListener;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class GridViewActivity extends AppCompatActivity implements Callback<GridEntity>, View.OnClickListener, OnPageLoadListener {

    private BroadcastHttpService service;
    private GridItemAdapter itemAdapter;
    private RecyclerView recycler;
    private TextView typeTextView;
    private ImageView backImageView;
    private int pageNum =1;
    private String categoryId;
    private String categoryName;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common);

        Intent intent = getIntent();
        categoryId = intent.getStringExtra("categoryId");
        categoryName = intent.getStringExtra("categoryName");
        type = intent.getStringExtra("type");
        itemAdapter = new GridItemAdapter(this,this);

                service = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(BroadcastHttpService.class);

        recycler = (RecyclerView) findViewById(R.id.bct_common_recycler);
        typeTextView = (TextView) findViewById(R.id.bct_common_type);
        backImageView = (ImageView) findViewById(R.id.bct_common_back);
        if (categoryName!=null) {
            typeTextView.setText(categoryName);
            service.getData(categoryId,pageNum,"20").enqueue(this);
        } else if (type != null){
            typeTextView.setText(type);
            service.getData(pageNum,"20").enqueue(this);
        }
        recycler.setAdapter(itemAdapter);
        backImageView.setOnClickListener(this);
    }

    @Override
    public void onResponse(Call<GridEntity> call, Response<GridEntity> response) {
        itemAdapter.addAll(response.body().getData().getData());
    }

    @Override
    public void onFailure(Call<GridEntity> call, Throwable t) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bct_common_back:
                finish();
                break;
        }
    }

    @Override
    public void onPageLoad() {
        pageNum++;
        service.getData(categoryId,pageNum,"20").enqueue(this);
    }
}
